1
//--------------------------------------------------------------------------
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 //--------------------------------------------------------------------------
11 open Microsoft.FSharp.Math
13 open System.Windows.Media
14 open System.ComponentModel
16 open System.Threading.Tasks
18 let epsilonValue = 1.0e-2
19 let nominalMass=1.0e-9<SI.kg
>
20 let gravity = PhysicalConstants.G
22 let mutable gNumParticles = 128
24 let timeStepSize = 100.0<SI.s
>
26 let drawParticleSizeScale = 0.2
27 let particleSystemCentroid = -1.0/1.0e8<SI.s
>
29 let defaultFocalColor = Colors.White
31 let mutable gNumCoresUsed = Environment.ProcessorCount
32 let op = new ParallelOptions()
33 op.MaxDegreeOfParallelism <- gNumCoresUsed
35 let mutable rand = new System.Random(4)
36 let rd() = (rand.NextDouble())
37 let rs() = (2.0 * ((rand.NextDouble()) - 0.5)) // random number between -1, 1
38 let qPosRnd () = rand.NextDouble()
39 let qNegRnd () = -1.0 * rand.NextDouble() // random number between -1, 0
43 val mutable px: float<SI.m
>
44 val mutable py: float<SI.m
>
45 val mutable vx: float<SI.m
/SI.s
>
46 val mutable vy: float<SI.m
/SI.s
>
48 val mutable mass: float<SI.kg
>
49 new(xpos
:float<SI.m
>, ypos
:float<SI.m
>,
50 xvel
:float<SI.m
/SI.s
>, yvel
:float<SI.m
/SI.s
>,
61 type point<[<Measure>]'u> =
62 val mutable x: float<'u
>
63 val mutable y: float<'u>
64 new(newx,newy) = { x = newx; y = newy}
65 static member (-) (p1:point<'u
>, p2
:point<'u>) = new point<'u
>(p1
.x-p2
.x, p1
.y-p2
.y)
74 /// Returns the enum quadrants values in an array
75 let enumeratedQuadrantsArray =
76 seq
{ for i
in 0..3 do yield enum
<quadrants>(i
)}
79 let spawnUniformly quadrantNumber
=
80 let initVel = 0.0<SI.m
/SI.s
>
81 match quadrantNumber
with
82 | quadrants.I ->particle( qPosRnd()*1.0<SI.m
>, qPosRnd()*1.0<SI.m
>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
83 | quadrants.II -> particle( qNegRnd()*1.0<SI.m
>,qPosRnd()*1.0<SI.m
>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
84 | quadrants.III -> particle( qNegRnd()*1.0<SI.m
>,qNegRnd()*1.0<SI.m
>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
85 | quadrants.IV -> particle( qPosRnd()*1.0<SI.m
>,qNegRnd()*1.0<SI.m
>, initVel, initVel, (rd() + 0.0) * nominalMass * 50000.0 )
86 | _ -> failwithf
"either you sent a root, or unknown quadrant matched"
88 let fillParticleArray i
=
90 | x when x < gNumParticles/4 -> spawnUniformly quadrants.I
91 | x when x < gNumParticles/2 -> spawnUniformly quadrants.II
92 | x when x < (3*gNumParticles)/4 -> spawnUniformly quadrants.III
93 | x when x <= gNumParticles -> spawnUniformly quadrants.IV
95 let mutable globalState = Array.init
gNumParticles (fun i
-> fillParticleArray i
)
98 rand <- new System.Random(4)
99 globalState <- Array.mapi
( fun i ele
-> fillParticleArray i
) globalState
101 let changeNumberOfParticles newNumber
=
102 rand <- new System.Random(4)
103 gNumParticles <- newNumber
104 globalState <- Array.init
newNumber (fun i
-> fillParticleArray i
)
107 let globalParticleList (arr
:particle array) =